package cn.learn.service.cargo.impl;

import cn.learn.dao.cargo.ContractDao;
import cn.learn.domain.cargo.Contract;
import cn.learn.domain.cargo.ContractExample;
import cn.learn.service.cargo.ContractService;
import cn.learn.vo.ContractProductVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ContractDao contractDao;

    @Override
    public Contract findById(String id) {
        Contract contract = contractDao.selectByPrimaryKey(id);
        return contract;
    }

    @Override
    public void save(Contract contract) {
        // 设置初始数据
        contract.setId(UUID.randomUUID().toString().replace("-", ""));
        contract.setState(0);
        contract.setProNum(0);
        contract.setExtNum(0);
        contract.setTotalAmount(0.0);
        contract.setCreateTime(new Date());
        contractDao.insertSelective(contract);
    }

    @Override
    public void update(Contract contract) {
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void delete(String id) {
        contractDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ContractExample example, int page, int size) {
        PageHelper.startPage(page, size);
        List<Contract> contracts = contractDao.selectByExample(example);
        PageInfo resultData = new PageInfo(contracts);
        return resultData;
    }

    @Override
    public List<ContractProductVo> findOutData(String companyId, String inputDate) {
        List<ContractProductVo> resultData = contractDao.findOutDate(companyId,inputDate);
        return resultData;
    }
}
